﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace LVTN
{
    public partial class PO_PurchaseForm : Form
    {
        private int totalMoney = 0;
        public PO_PurchaseForm()
        {
            InitializeComponent();
        }



        private void PurchaseForm_Load(object sender, EventArgs e)
        {

        }



        private void finishStrip_Click(object sender, EventArgs e)
        {
            Purchase purchase = new Purchase();
            totalMoney = 0;
            tlMoney.Text = totalMoney.ToString();
            foreach (DataGridViewRow row in InfoGrid.Rows)
            {
                if(row.Cells[0].Value == null)
                    break;
                
                var invtId = Int32.Parse(row.Cells[0].Value.ToString());
                var qty = Int32.Parse(row.Cells[3].Value.ToString());
                Inventory inventory = purchase.GetInventoryInfo(invtId);

                row.Cells[1].Value = inventory.Desc;
                row.Cells[2].Value = inventory.PurchaseUnit;
                row.Cells[4].Value = inventory.PurcharsePrice;
                row.Cells[5].Value = qty*inventory.PurcharsePrice;
                totalMoney += Int32.Parse(row.Cells[5].Value.ToString());
                tlMoney.Text = totalMoney.ToString();
            }

            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (typeTextBox.Text == "")
            {
                
            }
            var purchase = new Purchase();
            purchase.PoNumber = Int32.Parse(poNumTextBox.Text);

            purchase.CreatePurchaseOrder(Int32.Parse(vendorTextBox.Text), Char.Parse(typeTextBox.Text), DateTime.Parse(dateTextBox.Text), Int32.Parse(siteTextBox.Text));
            foreach (DataGridViewRow row in InfoGrid.Rows)
            {
                if (row.Cells[0].Value == null)
                    break;

                var invtId = Int32.Parse(row.Cells[0].Value.ToString());
                var qty = Int32.Parse(row.Cells[3].Value.ToString());
                Inventory inventory = purchase.GetInventoryInfo(invtId);

                purchase.CreateOrderDetail(invtId, qty);
            }
        }
    }
}
